// 路由拦截
import router from './routers';
import store from './store';
import { getUserInfo } from './services';
import notification from 'ant-design-vue/es/notification';

const whiteList = ['Login']     // 白名单

router.beforeEach( async(to, from, next) => {
    let token = localStorage.getItem('token');
    if(whiteList.includes(to.name)){
        next();
    }else{
        if(!store.state.user.info){
            // 获取用户信息
            let res = await getUserInfo();
            if(res.success){
                store.commit('SET_INFO', res.responseObject);
                // 获取用户权限
                let auth = res.responseObject.description;
               // 如果是制作报表页，判断有没有权限，没有则返回主页
                if(to.meta.permission && !to.meta.permission.includes(auth)){
                    next({ path: '/home', replace: true });
                    return;
                }
                next();
            }else{
                // 获取用户信息失败,跳转到登录页
                notification.error({
                    message: '错误',
                    description: '请求用户信息失败，请重试'
                })
                next({ path: '/', replace: true })
            }
        }else{
            next()
        }
    }
})